package pub.guoxin.oops.manager.handler.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import pub.guoxin.oops.common.sms.enums.SmsTemplate;
import pub.guoxin.oops.enums.ComparisonOperator;
import pub.guoxin.oops.manager.handler.AbstractOneBoundaryAlarmInfoHandler;
import pub.guoxin.oops.manager.vo.AlarmCacheVo;
import pub.guoxin.oops.model.AlarmInfo;
import pub.guoxin.oops.model.Communication;
import pub.guoxin.oops.model.CommunicationData;

import java.util.List;

import static pub.guoxin.oops.manager.handler.AlarmFieldNameConstants.AlarmInfoHandler;
import static pub.guoxin.oops.manager.handler.AlarmFieldNameConstants.COMMUNICATION_BATTERY;

/**
 * 通讯模块电量
 */
@Slf4j
@Service(COMMUNICATION_BATTERY + AlarmInfoHandler)
public class CommunicationBatteryAlarmInfoHandlerImpl extends AbstractOneBoundaryAlarmInfoHandler {

    @Override
    public List<AlarmInfo> handleRequest(CommunicationData communicationData, List<AlarmInfo> alarmInfos) {

        Communication communication = communicationData.getCommunication();
        Float batteryLevel = communication.getBatteryLevel();
        String communicationSn = communication.getSn();
        AlarmCacheVo result = getAlarmInfo(communicationSn, null, ComparisonOperator.LESS_THAN, COMMUNICATION_BATTERY);
        ComparisonOperator comparisonOperator = judgmentBoundaryValue(batteryLevel, COMMUNICATION_BATTERY);
        switch (comparisonOperator) {
            case NORMAL:
                dataNormal(result, communicationSn, null, ComparisonOperator.LESS_THAN, COMMUNICATION_BATTERY);
                break;
            case GREATER_THAN:
                log.error("逻辑错误，通讯模块电量没有大于");
                break;
            case LESS_THAN:
                communicationSnGreaterThanOrLessThan(result, communicationSn, null, batteryLevel, COMMUNICATION_BATTERY, SmsTemplate.COMMUNICATION_BATTERY_LEVEL, ComparisonOperator.LESS_THAN);
                break;
        }

        if (this.isNextHandler()) {
            this.getSuccessor().handleRequest(communicationData, alarmInfos);
        }
        return alarmInfos;
    }
}
